import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class SixCaseContainer extends JComponent implements MouseListener{
	static public int MAXCONTSIZE = 6;
	static public int ICONSIZE = 128;
	static public int SPACER = 20;
	public int DEFAUlT_VIEW = 1; // 6 cases, 0 if 1 case
	ItemBoxed[] items = {};
	Dimension d;
	JPanel grid = new JPanel(null); // null is used for setting layout as free
	NendoColor c;
	
	JLabel test1;
	JLabel test2;
	JLabel test3;
	JLabel test4;
	JLabel test5;
	JLabel test6;
	
	
	SixCaseContainer(ItemBoxed[] elem){
		this(elem,1);
	}
	
	SixCaseContainer(ItemBoxed[] elem,int view){
		this.items = elem;
		this.DEFAUlT_VIEW = view;
		this.d = new Dimension(350,500);
		this.setPreferredSize(d);
		init(elem);
	}
	
	
	public void init_old(){ //DEPRECATED
		//System.out.println(this.getInsets());
		//System.out.print(items[1].getColor(0));
		test1 = new JLabel("111");
		test2 = new JLabel("222");
		test3 = new JLabel("333");
		test4 = new JLabel("444");
		test5 = new JLabel("555");
		test6 = new JLabel("666");

		test1.setBackground(items[0].getColor(0));
		test2.setBackground(items[1].getColor(0));
		test3.setBackground(items[2].getColor(0));
		test4.setBackground(items[3].getColor(0));
		test5.setBackground(items[4].getColor(0));
		test6.setBackground(items[5].getColor(0));
		
		test1.setOpaque(true);
		test2.setOpaque(true);
		test3.setOpaque(true);
		test4.setOpaque(true);
		test5.setOpaque(true);
		test6.setOpaque(true);
		
		test1.setBounds(30, 30, 128, 128);
		test2.setBounds(190, 30, 128, 128);
		test3.setBounds(30, 185, 128, 128);
		test4.setBounds(190, 185, 128, 128);
		test5.setBounds(30, 340, 128, 128);
		test6.setBounds(190, 340, 128, 128);
		
		test1.addMouseListener(this);
		test2.addMouseListener(this);
		test3.addMouseListener(this);
		test4.addMouseListener(this);
		test5.addMouseListener(this);
		test6.addMouseListener(this);
		
		
		add(test1);
		add(test2);
		add(test3);
		add(test4);
		add(test5);
		add(test6);
	}
	
	public void init(ItemBoxed[] ib){

		JPanel grid = new JPanel();
		grid.setLayout(null); 
		for(int i=0; i < ib.length; i++){
			JLabel obj = new JLabel(ib[i].getName());
			obj.setBackground(ib[i].getColor(0));
			obj.setOpaque(true);
			obj.setBounds((30+(160*(i%2))), (30+(155*(i/2))), 128, 128);
			obj.addMouseListener(this);
			add(obj);
		}
	}
	
	public void deleteCurrentView(int view){
		DEFAUlT_VIEW = view;
		this.removeAll();
		this.validate();
		this.repaint();
	}
	
	public void changeCurrentView(ItemBoxed[] newbox, int view){
		deleteCurrentView(view);
		this.items = newbox;
		this.init(newbox);
		
}

	
	@Override
	public void paintComponent(Graphics g){
		int spacer = 20;
		int corner = 50;
		int h = super.getHeight();
		int w = super.getWidth();
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(Color.LIGHT_GRAY);
		g2d.fillRoundRect(spacer/2,spacer/2,w-spacer,h-spacer,corner,corner);
		g2d.setColor(Color.WHITE);
		g2d.fillRoundRect(spacer,spacer,w-(spacer*2),h-(spacer*2),corner/2,corner/2);
		g2d.setColor(Color.LIGHT_GRAY);
		if(DEFAUlT_VIEW == 1){
			g2d.fillRect(spacer, (h/3)+4, w-(spacer*2), 4);
			g2d.fillRect(spacer, ((2*h)/3)-8, w-(spacer*2), 4);
			g2d.fillRect((w/2)-5, spacer, 4, h-(spacer*2));
		}
	//	if(items.length > 0){
			
	//		for(int i=0; i < MAXCONTSIZE;i++){
	//			g2d.setColor(items[i].getColor(0));
	//			int posX = (w+(w*2*(i%2)))/4-(iconsize/2)+(spacer/2)-((i%2)*spacer);
	//			int posY = (((h/3)/2)+((i/2)*(h/3)))-(iconsize/2)+(((i/2)+1)%2*(spacer/2))-(spacer*(i/4));
				//System.out.println(posX+" , "+posY);
	//			g2d.fillRect(posX, posY ,iconsize, iconsize);
	//			g2d.setColor(Color.WHITE);
	//			FontRenderContext frc = g2d.getFontRenderContext();
	//			Font font = new Font("Arial", Font.BOLD,48);
	//			TextLayout tl = new TextLayout(items[i].getItemNum(),font,frc);
	//			tl.draw(g2d, posX+20, posY+iconsize-10);
				
				
	//		}
			
	//	}
	}

	public int positionCheck(Rectangle r){
		int i = 0;
		if (r.getX() > 150) i++;
		if(r.getY() > 150) i += 2;
		if(r.getY() > 300) i += 2;
		return i;
		
	}
	
	public JPanel returnGrid(){
		return grid;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		new ItemCard(items[positionCheck(((JLabel)e.getSource()).getBounds())]);
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		c = new NendoColor(((JLabel)e.getSource()).getBackground());
		((JLabel) e.getSource()).setBackground(c.alter());
	}

	@Override
	public void mouseExited(MouseEvent e) {
		c.revert();
		((JLabel) e.getSource()).setBackground(c.getColor());
	}

	@Override
	public void mousePressed(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}
		
	}

